package com.example.lib.socket;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;


public class MyServiceSocket extends Thread{
    public Mycallback call;
    public  void StartService(){
//        MyThread myThread = new MyThread();
//        myThread.start();

    }
    public void run(){
        // 线程执行的代码
        AcceptData();
    }
     public void AcceptData() {
         OcrData od=new OcrData();
         try {

             // 创建ServerSocket对象，指定监听的端口号

             InetAddress ia= Inet4Address.getByAddress(new byte[]{(byte)192,(byte)168,10,101});


            // ServerSocket serverSocket = new ServerSocket(9000,50,ia);
             ServerSocket serverSocket = new ServerSocket(9000);
             System.out.println("service is start");

             od.ImgStr="service is start";
             call.MyCall(od);

             // 监听客户端的连接请求
             Socket clientSocket = serverSocket.accept();
             System.out.println("client is success");
             od.ImgStr="client is success";
             call.MyCall(od);
             // 获取输入流和输出流 输入流和输出流是通过socket对象来进行数据传输的。
             InputStream inputs=clientSocket.getInputStream();


             //BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

             String message="";
             while (true) {
                 // 读取客户端发送的信息
//                 byte[] buf=new byte[1000];
//                 byte[] clientData=new byte[1000];
//                 int leng=0;
//                 int data = inputs.read(buf,0,100);
//                 System.out.println("data is " +data);
//                 while (data!=-1){
//                     for (int a=0;a<data;a++){
//                         if (buf[a]!=0){
//                             leng=leng+1;
//                             clientData[a]=buf[a];
//                         }else{
//                             break;
//                         }
//                     }
//                     message=new String(clientData,0,leng);
//                     System.out.println("client is " +message);
//                     break;
//                 }
                 byte[] buff=new byte[5000];
                 int count= inputs.read(buff);
                 HeadData hd=MySocketData.getHeadData(buff);
               //  message = new String(buff,hd.headLong+1,hd.dataLong);
                 //message = in.readLine();
                 System.out.println("client is " +message);
                 if (message.equalsIgnoreCase("exit")) {
                     // 如果接收到终止标志，退出循环
                     break;
                 }else{

                     od.ImgStr=message;
                     call.MyCall(od);
                 }
                 // 发送响应给客户端
                // message=MySocketData.getSendData("server:client msg is " + message);
                 out.println( message);
                 out.flush();
             }

             // 关闭连接
             clientSocket.close();
             serverSocket.close();
         } catch (IOException e) {
             od.ImgStr="find is error";
             call.MyCall(od);
             e.printStackTrace();
         }
     }
}
